package com.bootdo.lgs.orderbind.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.FileType;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.R;
import com.bootdo.lgs.orderbind.model.OrderBindQuery;
import com.bootdo.lgs.orderbind.model.OrderBindUpdate;
import com.bootdo.lgs.orderbind.model.OrderBindUpload;
import com.bootdo.lgs.orderbind.service.OrderBindService;
import com.bootdo.system.domain.UserDO;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author bootdo 1992lcg@163.com
 */
//@RequestMapping("/lgs/order")
@Controller
public class OrderBindController extends BaseController {
	//String prefix = "lgs";
	@Autowired
	OrderBindService orderBindService;

	@Autowired
	private FileService sysFileService;

	@Autowired
	private BootdoConfig bootdoConfig;

	//@RequiresPermissions("sys:menu:menu")
	@GetMapping("/lgs/orderBind/deal")
	String deal() {
		return "lgs/orderBind/deal";
	}

    @GetMapping("/lgs/orderBind/mng")
    String mng(OrderBindQuery condition) {
        return "lgs/orderBind/mng";
    }

    @GetMapping("/lgs/orderBind/mng/showEdit")
    String showEdit(OrderBindQuery condition) {
        return "lgs/orderBind/edit";
    }

    @GetMapping("/lgs/orderBind/deal/list")
    @ResponseBody
    List dealList(OrderBindQuery condition) {
        UserDO user = getUser();
        BeanUtils.copyProperties(user, condition);
        condition.setLgsStatus("3");
        return orderBindService.getList(condition);
    }

    @GetMapping("/lgs/orderBind/mng/list")
    @ResponseBody
    List mngList(OrderBindQuery condition) {
        UserDO user = getUser();
        BeanUtils.copyProperties(user, condition);
        condition.setLgsStatus("5");
        return orderBindService.getList(condition);
    }

    @Log("物流运单号-批量保存")
    @RequiresPermissions("lgs:orderBind:deal:batchSave")
    @PostMapping("/lgs/orderBind/deal/batchSave")
    @ResponseBody
    R batchSave(@RequestBody List<OrderBindUpdate> condition) {
        long idCount = condition.stream().map(OrderBindUpdate::getLgs_track_number).distinct().count();
        if (condition.size() > idCount) {
            return R.error("存在重复的物流运单号！");
        }

        for (OrderBindUpdate dto : condition) {
            String trackNumber = dto.getLgs_track_number();
            if (!orderBindService.checkTrackNumber(dto.getId(), trackNumber)) {
                return R.error("物流运单号" + trackNumber + "已经存在！");
            }
        }

        //修改之前判断是否为空
        orderBindService.batchUpdateLgsNumber(condition, getUser());
        return R.ok();
    }

    @Log("物流运单号-修改-保存")
    @RequiresPermissions("lgs:orderBind:mng:update")
    @PostMapping("/lgs/orderBind/mng/update")
    @ResponseBody
    R mngUpdate(OrderBindUpdate condition) {
        String trackNumber = condition.getLgs_track_number();
        if (!orderBindService.checkTrackNumber(condition.getId(), trackNumber)) {
            return R.error("物流运单号" + trackNumber + "已经存在！");
        }

        //修改之前判断是否为空
        UserDO user = getUser();
        orderBindService.updateLgsNumber(condition, user);
        return R.ok();
    }


    @ApiOperation(value = "列表-导出", notes = "导出，使用post表单方式提交、进行下载")
    @RequestMapping(value = "/lgs/orderBind/export", method = RequestMethod.POST)
    public void export(OrderBindQuery condition, HttpServletResponse response) {
        UserDO user = getUser();
        BeanUtils.copyProperties(user, condition);
        condition.setLgsStatus("5");
        orderBindService.exportMng(condition, response);
    }

    @Log("回退订单")
    @PostMapping("lgs/orderBind/rollback")
    @ResponseBody
    R rollback(@RequestParam Long orderId) {
        //修改之前判断是否存在
        UserDO loginUser = getUser();
        Map<String, Object> condition = Maps.newHashMap();
        condition.put("id", orderId);
        condition.put("update_user_id", loginUser.getUserId());
        condition.put("update_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        orderBindService.rollback(condition);
        return R.ok();
    }

	@Log("上传回单图片")
	@ResponseBody
	@PostMapping("/lgs/orderBind/upload")
	R upload(OrderBindUpload conditon) {
		MultipartFile file = conditon.getFile();
		String fileName = file.getOriginalFilename();
		fileName = FileUtil.renameToUUID(fileName);
		FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + fileName, new Date());
		try {
			FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath(), fileName);
		} catch (Exception e) {
			return R.error();
		}
		conditon.setReceipt(sysFile.getUrl());
		if (sysFileService.save(sysFile) > 0 && orderBindService.updateReceiptPic(conditon)) {
			return R.ok().put("fileName", sysFile.getUrl());
		}
		return R.error();
	}

    @Log("删除回单图片")
    @GetMapping("/lgs/orderBind/delReceipt")
    @ResponseBody
    R delReceipt(@RequestParam Long orderId) {
        //修改之前判断是否存在
        orderBindService.delReceipt(orderId);
        return R.ok();
    }

}
